/**
 * @file
 *           base64.h
 * @brief
 *          <fillin>purpose
 *
 * @par Author (last changes):
 *          - Herea Adrian
 *          
 * @par Description:
 *
 *
 * @par Module-History:
 * @verbatim
 *  Date        Author				Reason
 *  15.01.2010  Herea Adrian         Initial creation
 *
 * @endverbatim
 *
 * @par Copyright Notice:
 * @verbatim
 * Copyright (C) 2010
 * All Rights Reserved.
 * The reproduction, transmission or use of this document or its contents is
 * not permitted without express written authority.
 * Offenders will be liable for damages. All rights, including rights created
 * by patent grant or registration of a utility model or design, are reserved.
 **/

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;

namespace Client.Protocol
{
    class Protocol
    {
        static public string popChild(ref string protocol)
        {
            int posBeginEnd = protocol.IndexOf('>');
            string sBegin;
            sBegin = protocol.Substring(0, posBeginEnd + 1);
            protocol = protocol.Substring(posBeginEnd + 1);
            int posChildBeginStart = protocol.IndexOf('<');
            string sChild = "";

            if (posChildBeginStart != -1)
            {
                int posChildBeginEnd = protocol.IndexOf(">");
                if (posChildBeginEnd != -1)
                {
                    string sChildTag = protocol.Substring(0, posChildBeginEnd + 1);
                    string sChildEndTag = "";
                    if (sChildTag.IndexOf(" ")>=0)
                     sChildEndTag = "</" + sChildTag.Substring(1, sChildTag.IndexOf(" ") - 1) + ">";
                    else
                        sChildEndTag = "</" + sChildTag + ">";

                    int posChildEnd = protocol.IndexOf(sChildEndTag);
                    if (posChildEnd != -1)
                    {
                        sChild = protocol.Substring(0, (posChildEnd + sChildEndTag.Length));
                        protocol = protocol.Substring(posChildEnd + sChildEndTag.Length);
                    }
                }
                protocol = sBegin + protocol;
            }
            return sChild;
        }


        static public string nodeValue(ref string protocol)
        {
            int posBeginEnd = protocol.IndexOf(">");
            string sBegin = protocol.Substring(0, posBeginEnd + 1);				//begin tag
            string sValue;
            string sEnd = "</" + sBegin.Substring(1, sBegin.IndexOf(" ") - 1) + ">"; //end tag
            sValue = protocol.Substring(posBeginEnd + 1);
            int posEnd = sValue.IndexOf(sEnd);

            if (posEnd != -1)
            {
                sValue = sValue.Substring(0, posEnd);
            }
            else
            {
                sValue = "";
            }
            return sValue;
        }


        static public string nodeAtribute(ref string protocol)
        {
            string sVal = "";
            int posBegin = protocol.IndexOf("=\"");
            if (posBegin != -1)
            {
                sVal = protocol.Substring(posBegin + 2); // =" ,two char
                int posEnd = sVal.IndexOf('"');
                if (posEnd != -1)
                {
                    sVal = sVal.Substring(0, posEnd);
                }
                else
                {
                    sVal = "";
                }
            }
            return sVal;
        }

       static public string protocol(Command cmd)
        {
            string protocol;
            protocol = "<xstl>";
            protocol += "<cmd id=\"" + cmd.ID.ToString() + "\">";
            foreach (Param param in cmd.Params)
            {
                protocol += "<par name=\"" + param.Name + "\">" + param.Val + "</par>";
            }
            protocol += "</cmd>";
            protocol += "</xstl>";
            return protocol;
        }
        
      static  public Command command(string protocol) {
            Command cmd ;
            string command = popChild(ref protocol);
            string id =  nodeAtribute(ref command);
            cmd = new Command();

           // FileStream file = new FileStream("local.txt", FileMode.Create, FileAccess.Write);
          //  BinaryWriter sw = new BinaryWriter(file);
            int ID = Int32.Parse(id,System.Globalization.NumberStyles.Integer);
            
            cmd.ID= ID;
            string parametre = popChild(ref command) ;
            string sValue;
            string sName ;
           // sw.Write(command);
          //  sw.Write("\r\n");
            while (parametre != "") 
            {
                sName = nodeAtribute(ref parametre);
                sValue = nodeValue(ref parametre);
                Param pParam= new Param (sName,sValue);
                cmd.addParam(pParam);
                parametre = popChild(ref command) ;
               // sw.Write(sValue);
               // sw.Write("\r\n");
               // sValue = "";
               // sName ="";
            }
           // sw.Close();
           // file.Close();
          return cmd;
        }
        private Protocol() { ;}
    }
}
